<?php



/**
 * Class db
 * mysql 数据库二次封装类 基于PDO
 */
class db
{
    protected static $db;

    protected static $config;


    /**
     * @param array $config
     * 设置数据库
     */
    public static function set_config($config = []){
        if (empty($config)){
            $config  =  db;
        }
        self::$config = $config;
        self::$db     = new ye_mysql($config);
    }


    /**
     * 执行前检查数据库是否设置
     */
    public static function check(){
        if (empty(self::$db) || empty(self::$config)){
            self::set_config();
        }
    }


    /**
     * @param $table
     * @param string $where
     * @param string $filed
     * @return mixed
     * 查询数据
     */
    public static function select($table,$where = " 1 = 1 ",$filed = "*",$order = "",$group = ""){
        self::check();

        $db   = self::$db;
        $res  = $db->field($filed)->where($where)->order($order)->group($group)->select($table);
        return $res;
    }


    /**
     * @param $table
     * @param $insert_data
     * @return mixed
     * 新增数据
     */
    public static function insert($table,$insert_data){
        self::check();

        $db   = self::$db;
        $res  = $db->insert($table,$insert_data);;
        return $res;
    }


    /**
     * @param $table
     * @param $where
     * @return mixed
     * 删除数据
     */
    public static function delete($table,$where){
        self::check();

        $db   = self::$db;
        $res  = $db->where($where)->delete($table);
        return $res;
    }


    /**
     * @param $table
     * @param $where
     * @param $update_data
     * @return mixed
     * 修改数据
     */
    public static function update($table,$where,$update_data){
        self::check();

        $db   = self::$db;
        $db  = $db->where($where);
        $res = $db->update($table,$update_data);
        return $res;
    }


    /**
     * @param $sql
     * @return mixed
     * 执行sql语句
     */
    public static function query($sql){
        self::check();

        $db   = self::$db;
        $res  = $db->doSql($sql);
        return $res;
    }


    /**
     * @return mixed
     * 获取最后一次执行语句
     */
    public static function getLastSql(){
        self::check();

        $db   = self::$db;
        $res  = $db->getLastSql();
        return $res;
    }


    /**
     * @param $table
     * @param $where
     * @param string $filed
     * @param string $order
     * @param int $page
     * @param int $page_size
     * @return mixed
     * 取出固定数据
     */
    public static function _list($table,$where,$filed = "*",$order = " id desc",$group = '',$page = 0,$page_size = 15){
        self::check();

        $db   = self::$db;
        $res  = $db->where($where)->field($filed)->order($order)->group($group)->limit($page,$page_size)->select($table);
        return  $res;
    }

}